// pages/classify/children/detail/detail.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    commodity: {}
  },
  /**
   * 页面的初始数据
   */
  data: {
    coupon:[],
    commodity: {},
    commentdata: [],
    commodityinfor: [],
    specification: "",
    address: "",
    addresses: [],
    idx: false,
    isadd: false,
    userdata: [
      {
        name: "给***爱",
        data: "2020-10-26",
        text: "产品包装：产品包装很严实，超级结实，摔不坏，快递员送货上门",
        buy: "5L*4桶 整箱装 桶装水",
        img: [
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic86.nipic.com%2Ffile%2F20160102%2F20471249_114738238200_2.jpg&refer=http%3A%2F%2Fpic86.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636250270&t=efba3d6a291970656f804303411291a6",
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic86.nipic.com%2Ffile%2F20160102%2F20471249_114738238200_2.jpg&refer=http%3A%2F%2Fpic86.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636250270&t=efba3d6a291970656f804303411291a6",
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic86.nipic.com%2Ffile%2F20160102%2F20471249_114738238200_2.jpg&refer=http%3A%2F%2Fpic86.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636250270&t=efba3d6a291970656f804303411291a6",
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic86.nipic.com%2Ffile%2F20160102%2F20471249_114738238200_2.jpg&refer=http%3A%2F%2Fpic86.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636250270&t=efba3d6a291970656f804303411291a6",
        ]
      },
      {
        name: "b***6",
        data: "2020-05-24",
        text: "产品包装：产品包装很严实，超级结实，摔不坏，快递员送货上门",
        buy: "5L*4桶 整箱装 桶装水",
        img: [
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic86.nipic.com%2Ffile%2F20160102%2F20471249_114738238200_2.jpg&refer=http%3A%2F%2Fpic86.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636250270&t=efba3d6a291970656f804303411291a6",
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic86.nipic.com%2Ffile%2F20160102%2F20471249_114738238200_2.jpg&refer=http%3A%2F%2Fpic86.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636250270&t=efba3d6a291970656f804303411291a6",
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic86.nipic.com%2Ffile%2F20160102%2F20471249_114738238200_2.jpg&refer=http%3A%2F%2Fpic86.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636250270&t=efba3d6a291970656f804303411291a6",
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic86.nipic.com%2Ffile%2F20160102%2F20471249_114738238200_2.jpg&refer=http%3A%2F%2Fpic86.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636250270&t=efba3d6a291970656f804303411291a6",
        ]
      },
    ],
    len: '',
    isshow: false,
    isshow1: false,
    isquan: false,
    reduce: 0,
    questions: [
      { question: "请问给上楼吗", ping: "共有65条回答" },
      { question: "给送上楼吗", ping: "共有61条回答" }
    ],
    num: 1
  },
  coupon:function(){
    if(this.data.commodity.price * this.data.num >= this.data.coupon.achieve){
      this.setData({reduce:Number(this.data.coupon.reduce)});
    }else{
      console.log('不满足优惠金额，无法领取');
    }
  },
  look:function(){
    this.setData({ isquan: true })
  },
  // 点击弹出商品规格
  joinin: function () {
    this.setData({ isshow: true })
  },
  // 点击弹出地址
  chooseaddress: function () {
    this.setData({ isshow1: true })
  },
  // 点击黑色背景
  dn: function () {
    this.setData({ isshow: false })
    this.setData({ isshow1: false })
    this.setData({ isadd: false })
    this.setData({ isquan: false })
  },
  // 加入购物车
  tocar: function (e) {
    let thing = e.currentTarget.dataset.item;
    console.log(thing)
    let that = this;
    // 加入购物车列表
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/shoppingCarAddModify',
      data: {
        token: "3c35c3d0738a879f23926f4676b2874e",
        // token: "24c7528233c8f808d3fe79d8c009fff7",
        good_id: thing.good_id,
        num: that.data.num,
        price: Number(thing.price),
        money: that.data.num * Number(thing.price) - Number(that.data.reduce),
        sku: JSON.stringify(that.data.specification),
      },
      success(res) {
        console.log(res.data);
      },
      fail(res) {
        console.log(res.data);
      },
      method: "POST"
    });
    // 获取购物车长度
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/shoppingCarList?token=3c35c3d0738a879f23926f4676b2874e',
      success(res) {
        that.setData({ len: res.data.data.data.length });
      },
      method: "POST"
    });
    this.setData({ isshow: false })
  },
  // 关闭弹窗
  close: function () {
    this.setData({ isshow: false });
  },
  close1: function () {
    this.setData({ isshow1: false });
  },
  close2: function () {
    this.setData({ isadd: false });
  },
  close3: function () {
    this.setData({ isquan: false });
  },
  // 弹出添加地址
  addaddress: function () {
    this.setData({ isadd: true });
  },
  // 跳转到购物车
  shopcar: function () {
    wx.switchTab({
      url: '../../../../pages/shopCar/shopCar',
    })
  },
  // 选择地址单选按钮
  radiobtn: function (e) {
    this.setData({ ['addresses.[' + e.currentTarget.dataset.idx + '].status']: !this.data.addresses[e.currentTarget.dataset.idx].status });
    let aa = this.data.addresses;
    aa.forEach((element, index) => {
      if (index != e.currentTarget.dataset.idx) {
        element.status = false;
      } else {
        this.setData({ address: element.address })
      }
    });
    this.setData({ addresses: aa })
  },
  // 跳转到立即购买
  buy: function (e) {
    let thing = e.currentTarget.dataset.item;
    thing.num = this.data.num;
    thing.money = this.data.num * thing.price;
    thing.sku = this.data.specification;
    thing.reduce = this.data.reduce,
    console.log(thing);
    thing = JSON.stringify(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '/pages/shopCar/direct/direct?thing=' + thing,
    })
  },
  // 回到商品列表
  lastpage: function () {
    wx.navigateTo({
      url: '../shoplist/shoplist',
    })
  },
  // 商品数量加1
  add: function () {
    this.setData({ num: this.data.num + 1 });
  },
  // 商品数量减1
  sub: function () {
    if (this.data.num > 1) {
      this.setData({ num: this.data.num - 1 });
    }
  },
  // 选择sku
  checkin: function (e) {
    let index = e.currentTarget.dataset.index;
    let aindex = e.currentTarget.dataset.aindex;
    this.setData({ ['commodityinfor[' + index + '].content[' + aindex + '].status']: !this.data.commodityinfor[index].content[aindex].status });
    let arr = this.data.commodityinfor;
    this.setData({ ['specification[' + index + ']']: e.currentTarget.dataset.item })
    arr.forEach((element, i) => {
      if (i == index) {
        element.content.forEach((item, ai) => {
          if (ai != aindex) {
            item.status = false;
          }
        });
      }
    });
    this.setData({ commodityinfor: arr })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.request({
      // url: 'http://api_devs.wanxikeji.cn/api/goodInfo?good_id=1980',
      url: 'http://api_devs.wanxikeji.cn/api/goodInfo?good_id=' + options.id,
      success(res) {
        let data = JSON.parse(res.data.data.info[0].colour);
        data = data.map((item) => {
          if (typeof item == "string") { 
            return JSON.parse(item); 
          } else {
            return item;
          }

        });
        let strs = [];
        that.setData({ commodity: res.data.data });
        data.forEach((element, index) => {
          let arrs = []
          element.content.forEach((item, i) => {
            if (i == 0) {
              let arr = {
                name: item,
                status: true
              }
              arrs.push(arr)
              strs.push(item)
            } else {
              let arr = {
                name: item,
                status: false
              }
              arrs.push(arr)
            }
          });
          data[index].content = arrs
        });
        that.setData({ specification: strs });
        that.setData({ commodityinfor: data });
      },
      method: "POST"
    });
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/shoppingCarList?token=3c35c3d0738a879f23926f4676b2874e',
      success(res) {
        that.setData({ len: res.data.data.data.length });
      },
      method: "POST"
    });
    // 优惠券
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/couponList',
      success(res) {
        let date = new Date(res.data.data[res.data.data.length-1].created_time)
        let date1 = new Date(res.data.data[res.data.data.length-1].valid_period_time)
        let d = res.data.data[res.data.data.length-1];
        d.created_time = date.toLocaleDateString();
        d.valid_period_time = date1.toLocaleDateString()
        that.setData({coupon:d})
        console.log(d);
      },
      method: "POST"
    });
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/userAddressList?token=3c35c3d0738a879f23926f4676b2874e',
      success(res) {
        let add1 = [];
        res.data.data.forEach((element, index) => {
          let add = [];
          add.push(element.procince)
          add.push(element.city)
          add.push(element.area)
          let string = add.join(' ')
          if (index == 0) {
            add1.push({ address: string, status: true ,address_id:element.address_id})
          } else {
            add1.push({ address: string, status: false ,address_id:element.address_id })
          }
        });
        that.setData({ addresses: add1 });
        that.setData({ address: that.data.addresses[0].address });
      },
      method: "POST"
    });
    wx.request({
      // url: 'http://api_devs.wanxikeji.cn/api/commentList?good_id=1980',
      url: 'http://api_devs.wanxikeji.cn/api/commentList?good_id=' + options.id,
      success(res) {
        that.setData({ commentdata: res.data.data });
      },
      method: "POST"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})